<?php

class ManagerController extends BaseController
{
	public $leftmenusTitle = '常用操作';
	
	
	protected function beforeAction($action)
    {
    	
    	parent::beforeAction();
    	$this->leftmenus = array();    	
    	$this->leftmenus[] = array('href'=>$this->createUrl('manager/notice'),'menutext'=>'通知公告管理','isValid'=>Yii::app()->user->checkAccess('MENU2_TZGG'));
    	$this->leftmenus[] = array('href'=>$this->createUrl('manager/right'),'menutext'=>'权限组管理','isValid'=>Yii::app()->user->checkAccess('MENU2_QXZGL'));
    	$this->leftmenus[] = array('href'=>$this->createUrl('manager/user'),'menutext'=>'用户管理','isValid'=>Yii::app()->user->checkAccess('MENU2_YHGL'));
    	$this->leftmenus[] = array('href'=>$this->createUrl('manager/product'),'menutext'=>'商品管理','isValid'=>Yii::app()->user->checkAccess('MENU2_SPGL'));
    	Yii::app()->clientScript->registerCoreScript('jquery');
        return true;
    } 


	/**
	 * 通知公告管理
	 */
	public function actionNotice()
	{
		$this->title='通知管理';
		$model=new NoticeSearchForm;
		
		$criteria=new CDbCriteria(array(  
			   //'defaultorder'=>'trade_date',  
			   //'with'=>array('discntCode','inchannelCode','producttypeCode','product','tradeStaff','tradeMethod','tradeUserType'),
			)); 

		if(isset($_GET['NoticeSearchForm']))
		{
			$model->attributes=$_GET['NoticeSearchForm'];
		}
		
		else
		{
			$model->attributes=array(
			    'title'=>'',
			    'start_date'=>date('Y-m-d'),
			    'end_date'=>date('Y-m-d'),
			);
		}
		
		if($model->validate())
		{
			$criteria->addSearchCondition('title',$model->title);
			//$criteria->compare('create_time','>='.(isset($_POST['export'])?$model->old_start_date:$model->start_date));
			//$criteria->compare('create_time','<='.(isset($_POST['export'])?$model->old_end_date:$model->end_date));
		}
		
		$dataProvider=new CActiveDataProvider('Notice', array(
			    'criteria'=>$criteria,
			    'pagination'=>array(
			        'pageSize'=>Yii::app()->params['reportsPerPage'],
			    ),
			));
		$this->render('notice',array('model'=>$model,'dataProvider'=>$dataProvider));		
	}
	public function actionNoticedelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			Notice::model()->findByPk($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
	public function actionNewnotice()
	{
		$model = new Notice;
		
		if(isset($_POST['Notice']))
		{
			// we only allow deletion via POST request
			$model->attributes = $_POST['Notice'];
			$model->staff_id = Yii::app()->session['currenUser']->user_name;
			if($model->validate())
			{
				$model->save();
			}
			$this->redirect($this->createUrl('manager/notice'));
		}
		
		$this->render('newnotice',array('model'=>$model));		
	}
	/**
	 * 权限组管理
	 */
	public function actionRight()
	{
		$this->title='权限组管理';
		$model=new RightForm;
		if(isset($_POST['RightForm']))
		{
			$model->attributes = $_POST['RightForm'];
			if($_POST['channel']=='get')
			{
				$model->getRightsByRole();
			}
			else if($_POST['channel']=='save')
			{
				$model->setRights();
			}
		}
		$this->render('right',array('model'=>$model,));
	}
	/**
	 * 用户管理
	 */
	public function actionUser()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/depart'));
		$this->title='用户管理';
		$model=new UserSearchForm;
		$criteria=new CDbCriteria(array(  
			   //'defaultorder'=>'trade_date',  
			   //'with'=>array('discntCode','inchannelCode','producttypeCode','product','tradeStaff','tradeMethod','tradeUserType'),
			)); 

		if(isset($_GET['UserSearchForm']))
		{
			$model->attributes=$_GET['UserSearchForm'];
		}
		
		else
		{
			/*
			$model->attributes=array(
				'eparchy_code'=>Yii::app()->session['currenUser']->eparchy_code,
			    'depart_code'=>Yii::app()->session['currenUser']->depart_code,
			    'start_date'=>date('Y-m-d'),
			    'end_date'=>date('Y-m-d'),
			);
			*/
		}
		if($model->validate())
		{
			$criteria->addSearchCondition('eparchy_code',$model->eparchy_code);
			$criteria->addSearchCondition('depart_code',$model->depart_code);
			$criteria->addSearchCondition('user_name',$model->user_name);
			$criteria->addSearchCondition('phone',$model->phone);
		}
		Yii::log(CJSON::encode($criteria),'info');
		$dataProvider=new CActiveDataProvider('User', array(
			    'criteria'=>$criteria,
			    'pagination'=>array(
			        'pageSize'=>Yii::app()->params['reportsPerPage'],
			    ),
			));
		$this->render('userlist',array('model'=>$model,'dataProvider'=>$dataProvider,));
	}
	/**
	 * 用户管理
	 */
	public function actionUserdelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			User::model()->updateByPk($id,array('state_code'=>1));

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
	
	/**
	 * 用户管理
	 */
	public function actionUserupdate($id)
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/depart'));
		$model = User::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			if($model->validate())
			{
				$model->save();
				Yii::app()->user->setFlash('success','资料修改成功!');
			}
		}
		$this->render('userupdate',array(
			'model'=>$model,
		  ));
	}
	
/**
	 * 用户管理
	 */
	public function actionUsernew()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/depart'));
		$model = new User();
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			if($model->validate())
			{
				$model->save();
				Yii::app()->user->setFlash('success','资料新增成功!');
			}
		}
		$this->render('usernew',array(
			'model'=>$model,
		  ));
	}
	
	/**
	 * 产品管理
	 */
	public function actionProduct()
	{
		Yii::app()->clientScript->registerScriptFile(Yii::app()->createUrl('js/product'));
		$this->title='商品管理';
		$model=new ProductSearchForm;
		$criteria=new CDbCriteria(array(  
			   'with'=>array('producttypeCode',),
			)); 

		if(isset($_GET['ProductSearchForm']))
		{
			$model->attributes=$_GET['ProductSearchForm'];
		}
		
		else
		{
			
		}
		if(isset($model->producttype_code))
		{
			$criteria->join = 'JOIN lths_producttype_product on t.product_id=lths_producttype_product.product_id';
			$criteria->compare('lths_producttype_product.producttype_code',$model->producttype_code);
		}
		if($model->validate())
		{
			$criteria->addSearchCondition('t.product_id',$model->product_code);
			$criteria->addSearchCondition('t.product_name',$model->product_name);
		}
		
		$dataProvider=new CActiveDataProvider('Product', array(
			    'criteria'=>$criteria,
			    'pagination'=>array(
			        'pageSize'=>Yii::app()->params['reportsPerPage'],
			    ),
			));
		$this->render('productlist',array('model'=>$model,'dataProvider'=>$dataProvider,));
	}
	
	
	/**
	 * 商品管理
	 */
	public function actionProductupdate($id)
	{
		$model = Product::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		if(isset($_POST['Product']))
		{
			$model->attributes=$_POST['Product'];
			if($model->validate())
			{
				$model->save();
				Yii::app()->user->setFlash('success','修改成功!');
			}
		}
		$this->render('productupdate',array(
			'model'=>$model,
		  ));
	}
/**
	 * 产品管理
	 */
	public function actionProductnew()
	{
		$model = new Product;
		if(isset($_POST['Product']))
		{
			$model->attributes=$_POST['Product'];
			if($model->validate())
			{
				$model->save();
				Yii::app()->user->setFlash('success','新增成功!');
			}
		}
		$this->render('productnew',array(
			'model'=>$model,
		  ));
	}
	
	/**
	 * 权限初始化,上线后要注视掉
	 */
	public function actionAssign()
	{
		//http://localhost/lths/index.php/manager/assign
		$auth=Yii::app()->authManager;
		/*
		$auth->clearAll();
		$auth->clearAuthAssignments();
//权限定义 

		$auth->createOperation('MENU1_TZGG','一级菜单权限-通知公告');
		$auth->createOperation('MENU1_SJLR','一级菜单权限-数据录入');
		$auth->createOperation('MENU2_HYXSLR','二级菜单权限-合约销售录入');
		$auth->createOperation('MENU2_LJXSLR','二级菜单权限-裸机销售录入');
		$auth->createOperation('MENU2_CPWJLR','二级菜单权限-产品外借录入');
		$auth->createOperation('MENU2_SPQGLR','二级菜单权限-商品请购录入');

		$auth->createOperation('MENU1_TJBB','一级菜单权限-统计报表');
		$auth->createOperation('MENU2_HYXSRB','二级菜单权限-合约销售日报');
		$auth->createOperation('MENU2_LJXSRB','二级菜单权限-裸机销售日报');
		$auth->createOperation('MENU2_CPWJRB','二级菜单权限-产品外借日报');
		$auth->createOperation('MENU2_MLRB','二级菜单权限-毛利日报');
		$auth->createOperation('MENU2_SPQGD','二级菜单权限-商品请购单');
		
		$auth->createOperation('MENU1_ZLXG','一级菜单权限-资料修改');

		$auth->createOperation('MENU1_HTGL','一级菜单权限-后台管理');
		$auth->createOperation('MENU2_TZGG','二级菜单权限-通知公告');
		$auth->createOperation('MENU2_QXZGL','二级菜单权限-权限组管理');
		$auth->createOperation('MENU2_YHGL','二级菜单权限-用户管理');
		$auth->createOperation('MENU2_SPGL','二级菜单权限-商品管理');

        $auth->createOperation('MENU2_HYXSLR_WY','操作权限-往月合约销售录入');
        $auth->createOperation('MENU2_LJXSLR_WY','操作权限-往月裸机销售录入');
        $auth->createOperation('MENU2_CPWJLR_WY','操作权限-往月产品外借录入');
        $auth->createOperation('MENU2_SPQGLR_WY','操作权限-商品请购录入');
        
        $auth->createOperation('MENU2_HYXSRB_DS','操作权限-本地市合约销售日报查询');
        $auth->createOperation('MENU2_LJXSRB_DS','操作权限-本地市裸机销售日报');
        $auth->createOperation('MENU2_CPWJRB_DS','操作权限-本地市产品外借日报');
        $auth->createOperation('MENU2_MLRB_DS','操作权限-本地市毛利日报');
        $auth->createOperation('MENU2_SPQGD_DS','操作权限-本地市商品请购单');
        
        $auth->createOperation('MENU2_HYXSRB_QQ','操作权限-全区合约销售日报查询');
        $auth->createOperation('MENU2_LJXSRB_QQ','操作权限-全区裸机销售日报');
        $auth->createOperation('MENU2_CPWJRB_QQ','操作权限-全区产品外借日报');
        $auth->createOperation('MENU2_MLRB_QQ','操作权限-全区毛利日报');
        $auth->createOperation('MENU2_SPQGD_QQ','操作权限-全区商品请购单');
//角色定义
		$role=$auth->createRole('TRADER1','营业员1');
		$role=$auth->createRole('TRADER2','营业员2');
		$role=$auth->createRole('TRADER3','营业员3');
		$role=$auth->createRole('TRADER4','营业员4');
		$role=$auth->createRole('TRADER5','营业员5');
		$role=$auth->createRole('ADMIN1','管理员1');
		$role=$auth->createRole('ADMIN2','管理员2');
		$role=$auth->createRole('ADMIN3','管理员3');
		$role=$auth->createRole('ADMIN4','管理员4');

//角色权限分配
		$auth->roles['TRADER1']->addChild('MENU1_TZGG');
		$auth->roles['TRADER1']->addChild('MENU1_SJLR');
		$auth->roles['TRADER1']->addChild('MENU2_HYXSLR');
		$auth->roles['TRADER1']->addChild('MENU2_LJXSLR');
		$auth->roles['TRADER1']->addChild('MENU2_CPWJLR');
		$auth->roles['TRADER1']->addChild('MENU2_SPQGLR');

		$auth->roles['TRADER1']->addChild('MENU1_TJBB');
		$auth->roles['TRADER1']->addChild('MENU2_HYXSRB');
		$auth->roles['TRADER1']->addChild('MENU2_LJXSRB');
		$auth->roles['TRADER1']->addChild('MENU2_CPWJRB');
		$auth->roles['TRADER1']->addChild('MENU2_MLRB');
		$auth->roles['TRADER1']->addChild('MENU2_SPQGD');
		
		$auth->roles['TRADER1']->addChild('MENU1_ZLXG');
		
		$auth->roles['ADMIN1']->addChild('MENU1_TZGG');
		$auth->roles['ADMIN1']->addChild('MENU1_SJLR');
		$auth->roles['ADMIN1']->addChild('MENU2_HYXSLR');
		$auth->roles['ADMIN1']->addChild('MENU2_LJXSLR');
		$auth->roles['ADMIN1']->addChild('MENU2_CPWJLR');
		$auth->roles['ADMIN1']->addChild('MENU2_SPQGLR');
		$auth->roles['ADMIN1']->addChild('MENU1_TJBB');
		$auth->roles['ADMIN1']->addChild('MENU2_HYXSRB');
		$auth->roles['ADMIN1']->addChild('MENU2_LJXSRB');
		$auth->roles['ADMIN1']->addChild('MENU2_CPWJRB');
		$auth->roles['ADMIN1']->addChild('MENU2_MLRB');
		$auth->roles['ADMIN1']->addChild('MENU2_SPQGD');
		$auth->roles['ADMIN1']->addChild('MENU1_ZLXG');
		$auth->roles['ADMIN1']->addChild('MENU1_HTGL');
		$auth->roles['ADMIN1']->addChild('MENU2_TZGG');
		$auth->roles['ADMIN1']->addChild('MENU2_QXZGL');
		$auth->roles['ADMIN1']->addChild('MENU2_YHGL');
		$auth->roles['ADMIN1']->addChild('MENU2_SPGL');
		$auth->roles['ADMIN1']->addChild('MENU2_HYXSLR_WY');
		$auth->roles['ADMIN1']->addChild('MENU2_LJXSLR_WY');
		$auth->roles['ADMIN1']->addChild('MENU2_CPWJLR_WY');
		$auth->roles['ADMIN1']->addChild('MENU2_SPQGLR_WY');
		$auth->roles['ADMIN1']->addChild('MENU2_HYXSRB_DS');
		$auth->roles['ADMIN1']->addChild('MENU2_LJXSRB_DS');
		$auth->roles['ADMIN1']->addChild('MENU2_CPWJRB_DS');
		$auth->roles['ADMIN1']->addChild('MENU2_MLRB_DS');
		$auth->roles['ADMIN1']->addChild('MENU2_SPQGD_DS');
		$auth->roles['ADMIN1']->addChild('MENU2_HYXSRB_QQ');
		$auth->roles['ADMIN1']->addChild('MENU2_LJXSRB_QQ');
		$auth->roles['ADMIN1']->addChild('MENU2_CPWJRB_QQ');
		$auth->roles['ADMIN1']->addChild('MENU2_MLRB_QQ');
		$auth->roles['ADMIN1']->addChild('MENU2_SPQGD_QQ');
//用户角色分配
        */
        //$auth->createOperation('MENU1_KCFX','一级菜单权限-库存分析');
        $auth->roles['ADMIN1']->addChild('MENU1_KCFX');
		//$auth->assign('ADMIN1',Yii::app()->user->id);

		echo 'The privilege has been assigned!';
		Yii::app()->end();
	}

}